package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.text.Annotation;
import java.util.Date;

@Service
@Slf4j
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.判断前端传过来的数据是否为空
        if (dto == null) {
            ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            return responseResult;
        }
        //2.分页参数检查
        dto.checkParam();
        //3.按名称模糊分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        //4.创建查询构造器
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            //如果前端传过来的数据的名字不为空的话则进行模糊查询
            lambdaQuery.like(AdChannel::getName, dto.getName());
        }
//        //5.查询完之后对查询的结果进行排序
//        lambdaQuery.orderByAsc(AdChannel::getOrd);
        //6.将构造好的页面和查询的数据进行组合
        IPage result = this.page(page, lambdaQuery);
        //7.对查询的结果进行封装
        PageResponseResult result1 = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
        result1.setData(result.getRecords());
        //8.将结果进行返回
        return result1;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
       //1.判断从前端传过来的数据是否为空或者是name的值是否为空，如果满足其中一个，则报缺少参数
//        if (adChannel==null || StringUtils.isBlank(adChannel.getName())) {
//            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
//        }
       //2.判断该频道是否存在
       int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,adChannel.getName()));
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        //3.设置其插入数据的时间
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        //4.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.如果前端传过来的参数为空的话或者参数的id为空的话则判断不合法
        if (adChannel==null || adChannel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.执行修改(通过前端传过来的这个对象的id找到数据库的数据对象)
        AdChannel channel = this.getById(adChannel.getId());
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"频道信息不存在");
        }
        //3.检验名称唯一性
        if (StringUtils.isNotBlank(adChannel.getName()) && !adChannel.getName().equals(channel.getName())){
            int count=this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,adChannel.getName()));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在！");
            }
        }
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.判断前端传过来的这个id数据库中是否存在数据
        AdChannel channel = this.getById(id);
//        int i=1/0;模拟异常的代码
        if (channel==null) {
            //return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"该数据不存在！");
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID,"该数据不存在");
        }
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
       removeById(channel);




        return null;
    }
}
